package com.sbs.gaoxinqutoilet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.entity.HomeMenuEntity;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2019/10/29.
 * Time: 2019/10/29  10:47
 */
public class SeckillListViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 上下文
     */
    private Context myContext;
    private List<HomeMenuEntity> mItemBeanList;

    public SeckillListViewAdapter(Context myContext, List<HomeMenuEntity> mItemBeanList) {
        this.myContext = myContext;
        this.mItemBeanList = mItemBeanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ItemViewHolder(LayoutInflater.from(myContext).inflate(R.layout.vlayout_chanel_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        final ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
        final HomeMenuEntity seckillBean = mItemBeanList.get(i);

        switch(seckillBean.getFunc_id()){
            case "201":
                //helper.getView(R.id.image_home_menu).setBackgroundResource(R.drawable.file);
                Glide.with(myContext).load(R.drawable.file_new)
                        .into(itemViewHolder.ivFigure);
                itemViewHolder.tvCoverPrice.setText("档案管理");

                break;
            case "205":
                //helper.getView(R.id.image_home_menu).setBackgroundResource(R.drawable.home_rebuild);
                Glide.with(myContext).load(R.drawable.home_shigong)
                        .into(itemViewHolder.ivFigure);
                itemViewHolder.tvCoverPrice.setText("施工登记");

                break;
            case "202":
                //helper.getView(R.id.image_home_menu).setBackgroundResource(R.drawable.acceptance);
                Glide.with(myContext).load(R.drawable.home_accept)
                        .into(itemViewHolder.ivFigure);

                itemViewHolder.tvCoverPrice.setText("户厕验收");
                break;
            case "401":
                //helper.getView(R.id.image_home_menu).setBackgroundResource(R.drawable.filecheck);
                Glide.with(myContext).load(R.drawable.home_check)
                        .into(itemViewHolder.ivFigure);

                itemViewHolder.tvCoverPrice.setText("户厕检查");
                break;
            case "306":
                //helper.getView(R.id.image_home_menu).setBackgroundResource(R.drawable.tongji);
                Glide.with(myContext).load(R.drawable.home_tongji)
                        .into(itemViewHolder.ivFigure);

                itemViewHolder.tvCoverPrice.setText("户厕统计");
                break;

            default:

                break;
        }



        itemViewHolder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = itemViewHolder.getLayoutPosition();//在增加数据或者减少数据时候，position和index就不一样了
                if (mOnItemClickLitener != null) {
                    mOnItemClickLitener.onItemClick(seckillBean);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mItemBeanList == null ? 0 : mItemBeanList.size();
    }


    /**
     * 声明列表项ViewHolder
     */
    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivFigure;
        private TextView tvCoverPrice;
        private LinearLayout ll_root;

        public ItemViewHolder(View view) {
            super(view);
            ivFigure = (ImageView) itemView.findViewById(R.id.img_thumb);
            tvCoverPrice = (TextView) itemView.findViewById(R.id.tv_title);
            ll_root = (LinearLayout) itemView.findViewById(R.id.layout_hori);
        }

    }


    /*=====================添加OnItemClickListener回调================================*/
    public interface OnHorizontalListItemClickLitener {
        /**
         * 列表项的点击事件
         */
        void onItemClick(HomeMenuEntity itemBean);
    }

    private OnHorizontalListItemClickLitener mOnItemClickLitener;

    public void setOnHorizontalListItemClickLitener(OnHorizontalListItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
